package com.info33.platform.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.SysUtil;
import com.info33.platform.system.dto.SysAppMenuDTO;
import com.info33.platform.system.entity.SysAppMenu;
import com.info33.platform.system.service.SysAppMenuService;
import com.info33.platform.system.vo.SysAppMenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/SysAppMenu")
@Api(tags = "系统 - 管理端 app - 菜单")
public class SysAppMenuController extends BaseController<SysAppMenuService, SysAppMenu> {

    private final SysAppMenuService sysAppMenuService;

    @Autowired
    public SysAppMenuController(SysAppMenuService sysAppMenuService) {
        super(sysAppMenuService);
        this.sysAppMenuService = sysAppMenuService;
    }

    @GetMapping(value = "/tree")
    @ApiOperation(value = "pc - 菜单树")
    public Result<List<SysAppMenuVO>> pcTree() {
        return Result.ok(sysAppMenuService.pcTree());
    }

    @PostMapping(value = "/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 菜单分页", notes = "入参说明：<br/>" +
            "pid = 1：顶级菜单<br/>" +
            "pid = ?：二级菜单（menuVO.id 取值）")
    public Result<IPage<SysAppMenu>> pcPage(
            @PathVariable int pageNum,
            @PathVariable int pageSize,
            @RequestBody SysAppMenuDTO dto) {
        return Result.ok(sysAppMenuService.page(new Page<>(pageNum, pageSize), new QueryWrapper<SysAppMenu>().eq(TableConstant.PID, dto.getPid())));
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "pc - 新增菜单", notes = "仅超级管理员可以新增")
    public Result<Object> pcAdd(
            @RequestBody SysAppMenu appMenu) {
        if (Boolean.FALSE.equals(SysUtil.isSysAdmin())) {
            return Result.error("操作失败，原因：权限不足");
        }
        return sysAppMenuService.pcAdd(appMenu);
    }

    @DeleteMapping(value = "/delete/{mid}")
    @ApiOperation(value = "pc - 删除", notes = "mid：app_menu id")
    public Result<Object> pcDelete(
            @PathVariable(value = "mid") Long mid) {
        return sysAppMenuService.pcDelete(mid);
    }

}